import 'dart:convert' show json;

import 'package:freedom/util/data_type.dart';

class MahjongRule {
  MahjongRule(
      {this.userCount,
      this.numberGame,
      required this.rules,
      this.turns,
      this.type});

  MahjongRule.init(
      {this.userCount,
      this.numberGame,
      required this.rules,
      this.turns,
      this.type});

  factory MahjongRule.empty() => MahjongRule.init(
      numberGame: 4, turns: 4, userCount: 4, rules: [], type: 1);
  factory MahjongRule.fromJson(Map<String, dynamic>? jsonRes) {
    if (jsonRes == null) {
      return MahjongRule.empty();
    }

    final List<String> rules = jsonRes['rules'] is List ? <String>[] : [];

    for (final dynamic item in jsonRes['rules']) {
      if (item != null && asT<String>(item) != null) {
        rules.add(asT<String>(item)!);
      }
    }

    return MahjongRule(
      userCount: asT<int>(jsonRes['userCount']),
      turns: asT<int>(jsonRes['turns']),
      type: asT<int>(jsonRes['type']),
      numberGame: asT<int>(jsonRes['numberGame']),
      rules: rules,
    );
  }

  int? userCount;
  int? numberGame;
  int? turns;
  int? type;
  List<String> rules = [];

  Map<String, dynamic> toJson() => <String, dynamic>{
        'userCount': userCount,
        'turns': turns,
        'type': type,
        'numberGame': numberGame,
        'rules': rules,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
